<template>
  <div>

    <el-form 
      ref="form" :model="form" :rules="rules"
      label-width="100px" style="width:700px;">
      <el-form-item label="房源名称" prop="cinema_name">
        <el-input v-model="form.house_name"></el-input>
      </el-form-item>
      <el-form-item label="选择位置">
        <div id="main" style="border:1px solid #444; width:100%; height:200px;"></div>
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input v-model="form.address"></el-input>
      </el-form-item>
      <el-form-item label="省份" prop="province">
        <el-input v-model="form.province"></el-input>
      </el-form-item>
      <el-form-item label="城市" prop="city">
        <el-input v-model="form.city"></el-input>
      </el-form-item>
      <el-form-item label="地区" prop="district">
        <el-input v-model="form.district"></el-input>
      </el-form-item>
      <el-form-item label="经度" prop="longitude">
        <el-input v-model="form.longitude"></el-input>
      </el-form-item>
      <el-form-item label="纬度" prop="latitude">
        <el-input v-model="form.latitude"></el-input>
      </el-form-item>
     
      
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即新增</el-button>
        <el-button @click="reseted">取消</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
import AMapLoader from '@amap/amap-jsapi-loader';
import axios from 'axios';


window._AMapSecurityConfig = {
    securityJsCode:'8ed50bf9433d47d5651cb9e8422a2059',
}

export default {
  data() {
    return {
        
      form: {
        house_name: '',
        address: '',
        province: '',
        city: '',
        district: '',
        longitude: '',
        latitude: '',
        tags: []
      },
      rules: {
        house_name:[{required:true, message:'必填', trigger:'blur'}],
        address:[{required:true, message:'必填', trigger:'blur'}],
        province:[{required:true, message:'必填', trigger:'blur'}],
        city:[{required:true, message:'必填', trigger:'blur'}],
        district:[{required:true, message:'必填', trigger:'blur'}],
        longitude:[{required:true, message:'必填', trigger:'blur'}],
        latitude:[{required:true, message:'必填', trigger:'blur'}],
        tags:[{required:true, message:'必填', trigger:'change'}],
      }
    }
  },

  methods: {

    /** 提交表单时执行 */
    onSubmit(){
      this.$refs['form'].validate(async(valid)=>{
        if(valid){
        let data=await axios.post("map",this.form)
        this.$message({
            message:"地图房源添加成功",
            type:'success'
        })
        //重置表单  
        this.reseted()
        }else{
            console.log("添加失败"+data.msg);
        }
      })
    },
    reseted(){
         this.$refs['form'].resetFields()
    },

    initMap(){ //初始化地图
        AMapLoader.load({
            key:"a531460d5b9a0d26b1d529df38fd03cd",             // 申请好的Web端开发者Key，首次调用 load 时必填
            version:"2.0",      // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
            plugins:['AMap.Geocoder'],       // 需要使用的的插件列表，如比例尺'AMap.Scale'等
        }).then((AMap)=>{
            this.map = new AMap.Map("main",{  //设置地图容器id
                viewMode:"3D",    //是否为3D地图模式
                zoom:12,           //初始化地图级别
                center:[108.935226,34.24518], //初始化地图中心点位置
            }); 
            this.map.on('click',(ev)=>{
                let {lng,lat}=ev.lnglat //获取经纬度
                console.log(`${lng},${lat }`);
                //当获取用户选中的坐标点后，执行逆地址解析
                let coder=new AMap.Geocoder()
                coder.getAddress([lng,lat],(status,result)=>{
                  console.log(result);
                  //回显示表单
                   let info=result.regeocode
                   this.form.address=info.formattedAddress
                   this.form.province=info.addressComponent.province
                   this.form.city=info.addressComponent.city
                   this.form.district=info.addressComponent.district
                   this.form.longitude=lng
                   this.form.latitude=lat
                   

                })
            })
        })
    },
  },

  mounted(){
    this.initMap() // 初始化地图
    
  }

};
</script>

<style lang="scss" scoped></style>
